package com.pureut.storage.util;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pureut.storage.domain.PublicMaterial;
import com.pureut.storage.dto.UnitConversionDto;
import com.pureut.storage.mapper.PublicMaterialMapper;
import com.sale.system.api.RemoteInventoryInquiryService;
import com.sale.system.api.RemoteUnitService;
import com.sale.system.api.domain.SysAuxiliaryUnitDetail;
import com.sale.system.api.domain.dto.SysAuxiliaryUnitDateDetailResultDto;
import com.sale.system.api.model.MaterialUnitDto;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * 单位换算工具类
 */
@Configuration
public class UnitUtil {

    @DubboReference
    RemoteInventoryInquiryService remoteInventoryInquiryService;

    @DubboReference
    RemoteUnitService remoteUnitService;

    @Resource
    PublicMaterialMapper publicMaterialMapper;

    /**
     * 单位换算通用方法
     *
     * @param materialMoreUnit 多单位
     * @param multiplyUnit     数量
     * @return
     * @throws Exception
     */
    public UnitConversionDto unitConversion(String materialMoreUnit, String multiplyUnit, Long materialId) throws Exception {

        UnitConversionDto unitConversionDto = new UnitConversionDto();
        if (materialMoreUnit != null) {
            SysAuxiliaryUnitDateDetailResultDto materialUnitById = remoteInventoryInquiryService.getMaterialUnitById(Long.valueOf(materialMoreUnit), materialId);

            // 获取逗号前的基础单位
            int str = materialUnitById.getAuxiliaryUnitName().indexOf(',');
            String substring = materialUnitById.getAuxiliaryUnitName().substring(0, str);
            long c = (long) (Double.parseDouble(String.valueOf(multiplyUnit)));
            MaterialUnitDto materialUnitDto = remoteInventoryInquiryService.selectMaterialUnitList(Long.valueOf(materialMoreUnit), c);
            //多单位
            unitConversionDto.setMaterialMoreUnit(materialUnitById.getAuxiliaryUnitName());

            //多单位数量
            unitConversionDto.setMaterialMoreUnitNumber(materialUnitDto.getUnitName());
            //单位
            unitConversionDto.setUnitDict(substring);
            return unitConversionDto;
        }
//        } else {
////            //单位
////            unitConversionDto.setUnitDict(unitByCodeToMap.get(materialUnit).getUnitName());
////        }
        return unitConversionDto;
    }

    /**
     * 获取某个多单位之前或者之后的转换
     */
    public UnitConversionDto atWillunitConversion(String materialMoreUnit, String materialCode, String multiplyUnit, Long materialId) throws Exception {

        UnitConversionDto unitConversionDto = new UnitConversionDto();

        //判断该物料有没有多单位
        PublicMaterial publicMaterial = publicMaterialMapper.selectOne(new QueryWrapper<PublicMaterial>()
            .lambda().eq(PublicMaterial::getMaterialCode, materialCode));
        if (publicMaterial.getMaterialMoreUnit() != null) {

            SysAuxiliaryUnitDateDetailResultDto materialUnitById = remoteInventoryInquiryService.getMaterialUnitById(Long.valueOf(materialMoreUnit), materialId);
//            if (materialUnitById.getUnitName() != null) {
//                //单位
//                unitConversionDto.setUnitDict(materialUnitById.getUnitName());
//            }
            if (materialUnitById.getMoreUnitName() != null) {
//                // 获取逗号前的基础单位
//                int str = materialUnitById.getAuxiliaryUnitName().indexOf(',');
//                String substring = materialUnitById.getAuxiliaryUnitName().substring(0, str);
                long c = (long) (Double.parseDouble(String.valueOf(multiplyUnit)));
                MaterialUnitDto materialUnitDto = remoteInventoryInquiryService.selectAtWillMaterialUnitList(materialId, c);
                //多单位
                unitConversionDto.setMaterialMoreUnit(materialUnitById.getAuxiliaryUnitName());
                //多单位数量
                unitConversionDto.setMaterialMoreUnitNumber(materialUnitDto.getUnitName());
                //单位
                unitConversionDto.setUnitDict(materialUnitById.getMoreUnitName());
                return unitConversionDto;
            }
        }
        return unitConversionDto;
    }

    /**
     * 单位转换
     */
    public String unitC(String materialCode,String number,Long materialId,String unitName){
        //根据物料id获取多单位
        List<SysAuxiliaryUnitDetail> list1 = remoteUnitService.getUnitByMaterialId(materialId);
        //定义多单位数量
        String moreUnit = "";
        String num = number;
        if (list1 != null && list1.size() != 0) {
            for (SysAuxiliaryUnitDetail sysAuxiliaryUnitDetail : list1) {
                String unitQuantity = sysAuxiliaryUnitDetail.getUnitQuantity();
                BigDecimal number1 = new BigDecimal(num);
                BigDecimal number2 = new BigDecimal(unitQuantity);
                BigDecimal[] bigDecimals = number1.divideAndRemainder(number2);
                // 数组的第一个值为商，第二个值为余数
                String discuss = bigDecimals[0].toString();
                String remainder = bigDecimals[1].toString();
                //如果直接被整除说明刚好满足当前最大单位
                if ("0".equals(remainder)) {
                    moreUnit = moreUnit + discuss + sysAuxiliaryUnitDetail.getUnitName();
                    break;
                } else {
                    if (sysAuxiliaryUnitDetail == list1.get(list1.size() - 1)) {
                        moreUnit = moreUnit + remainder + unitName;
                    }
                    if ("0".equals(discuss)) {
                        continue;
                    }
                    //没有被整除
                    moreUnit = moreUnit + discuss + sysAuxiliaryUnitDetail.getUnitName();
                    //修改num的值为当前的余数
                    num = remainder;
                }
            }
            //如果多单位数量为“”说明不满足其他的辅助单位,只满足最小单位
            if ("".equals(moreUnit)) {
                moreUnit = number + "" + unitName;
            }
        }
        return moreUnit;
    }

}
